#include "gtest/gtest.h"

#include "ObjectTest.h"
#include "helper/ScopedMemPtr.h"

class StringTest : public ObjectTest {};


using namespace helper;
using namespace object;
TEST_F(StringTest, testCreateFromCstr){
    const char * expect = "c_str";
    auto pObj = helper::ScopedMemPtr<String>(getObjectFactory()->createString(expect));
    ASSERT_EQ(expect, pObj->getValue());

    ASSERT_EQ(Object::STRING, pObj->getType());
    ASSERT_TRUE(pObj->isString());

    ASSERT_EQ(strlen(expect), (size_t)pObj->size());
}
TEST_F(StringTest, testCreateFromStdString){
    std::string expect("string");
    auto pObj = helper::ScopedMemPtr<String>(getObjectFactory()->createString(expect));
    ASSERT_EQ(expect, pObj->getValue());

    ASSERT_EQ(expect.size(), (size_t)pObj->size());
}
TEST_F(StringTest, testCreateFromBuffer){
    const char * expect = "buffer";
    size_t expectLen = strlen(expect);
    auto pObj = helper::ScopedMemPtr<String>(getObjectFactory()->createString(expect, 0, expectLen));
    ASSERT_EQ(expect, pObj->getValue());

    ASSERT_EQ(expectLen, (size_t)pObj->size());
}
TEST_F(StringTest, testCreateFromBufferLen){
    const char * expect = "buffer";
    size_t expectLen = strlen(expect);
    auto pObj = helper::ScopedMemPtr<String>(getObjectFactory()->createString(expect, expectLen));
    ASSERT_EQ(expect, pObj->getValue());

    ASSERT_EQ(expectLen, (size_t)pObj->size());
}
TEST_F(StringTest, testCreateFromLenChar){
    const char expect = 'c';
    size_t expectLen = 10;
    auto pObj = helper::ScopedMemPtr<String>(getObjectFactory()->createString(expectLen, expect));

    ASSERT_EQ(std::string(expectLen, expect), pObj->getValue());

    ASSERT_EQ(expectLen, (size_t)pObj->size());
}

TEST_F(StringTest, testAppend){
    const char * expect = "c_str+new";
    const char * old = "c_str";
    const char * new_str = "+new";
    auto pObj = helper::ScopedMemPtr<String>(getObjectFactory()->createString(old));
    pObj->append(new_str);
    ASSERT_EQ(expect, pObj->getValue());
}

TEST_F(StringTest, testClear){
    const char * expect = "clear_before_string";
    auto pObj = helper::ScopedMemPtr<String>(getObjectFactory()->createString(expect));
    ASSERT_EQ(strlen(expect), (size_t)pObj->size());

    ObjectTest::testClear(pObj.get());
}

